package com.modakai.med.hosp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.modakai.med.common.MD5;
import com.modakai.med.common.result.Result;
import com.modakai.med.enums.HospitalSetStatusEnum;
import com.modakai.med.hosp.service.HospitalSetService;
import com.modakai.med.model.hosp.HospitalSet;
import com.modakai.med.vo.HospitalSetQueryVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@RequiredArgsConstructor
public class HospitalSetController {

    private final HospitalSetService hospitalSetService;

    @Operation(summary = "查询所有医院设置")
    @GetMapping("findAll")
    public Result<List<HospitalSet>> findAllHospitalSet() {
        List<HospitalSet> hospitalSetList = hospitalSetService.list();
        return Result.ok(hospitalSetList);
    }

    @Operation(summary = "逻辑删除医院设置")
    @GetMapping("removeHospitalSet")
    public Result<String> removeHospitalSet(Long id) {
        boolean isSuccess = hospitalSetService.removeById(id);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @Operation(summary = "带条件分页查询")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result<Page<HospitalSet>> findPageHospSet(@PathVariable(name = "current") long current,
                                  @PathVariable(name = "limit") long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo queryData) {

        // 1 创建page对象
        Page<HospitalSet> page = new Page<>(current, limit);
        // 2 构造查询条件
        // 3 调用方法实现分页查询
        Page<HospitalSet> resultPage = hospitalSetService.lambdaQuery()
                .eq(StringUtils.isNotBlank(queryData.getHosname()), HospitalSet::getHosname, queryData.getHosname())
                .eq(StringUtils.isNotBlank(queryData.getHoscode()), HospitalSet::getHoscode, queryData.getHoscode())
                .page(page);

        // 4 返回结果
        return Result.ok(resultPage);
    }

    @Operation(summary = "添加医院设置")
    @PostMapping("saveHospitalSet")
    public Result<String> saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        hospitalSet.setStatus(HospitalSetStatusEnum.USE.getStatus());
        // 签名秘钥 使用 md5
        String cryptKey = System.currentTimeMillis() + "" + Math.random() * 1000000000;
        hospitalSet.setSignKey(MD5.encrypt(cryptKey));

        // 存在事务问题
        boolean isSuccess = hospitalSetService.save(hospitalSet);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @Operation(summary = "根据id获取医院设置")
    @GetMapping("getHospSet/{id}")
    public Result<HospitalSet> getHospSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //6 修改医院设置
    @Operation(summary = "修改医院设置")
    @PostMapping("updateHospitalSet")
    public Result<String> updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //7 批量删除医院设置
    @Operation(summary = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result<String> batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    @Operation(summary = "医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result<Object> lockHospitalSet(@PathVariable Long id, @PathVariable Integer status) {
        // 1. 根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);

        if (ObjectUtils.isEmpty(hospitalSet))
            return Result.fail().message("查询不到对应的医院信息");
        // 2. 修改状态
        // 2.1 判断是否为系统内部的状态
        HospitalSetStatusEnum.validateStatus(status);

        // 3、修改医院信息
        hospitalSetService.lambdaUpdate()
                .eq(HospitalSet::getId, hospitalSet.getId())
                .set(HospitalSet::getStatus, status)
                .update();

        return Result.ok();
    }

    @Operation(summary = "发送签名密钥")
    @PutMapping("sendKey/{id}")
    public Result<String> pushHospitalSing(@PathVariable long id) {
        // 1. 根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        // 2. TODO 发送短信
        // 3. 对接管理平台接口
        return Result.ok();
    }
}

